package com.yjc.drygoods.coupon.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yjc.common.exception.BizCodeEnume;
import com.yjc.common.to.CouponAndMemberTo;
import com.yjc.common.to.CouponTo;
import com.yjc.common.vo.MemberRespVo;
import com.yjc.drygoods.coupon.entity.CouponEntity;
import com.yjc.drygoods.coupon.service.CouponHistoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yjc.drygoods.coupon.entity.CouponHistoryEntity;
import com.yjc.common.utils.PageUtils;
import com.yjc.common.utils.R;


/**
 * 优惠券领取历史记录
 *
 * @author yjc
 * @email yjc19979401754@126.com
 * @date 2022-08-19 23:06:24
 */
@Slf4j
@RestController
@RequestMapping("coupon/couponhistory")
public class CouponHistoryController {
    @Autowired
    private CouponHistoryService couponHistoryService;

    @GetMapping("/get/coupon/byMmeberId")
    public R getCouponByMemberId(@RequestParam("memberId") Long memberId) {
        List<CouponEntity> coupons = couponHistoryService.getCouponByMemberId(memberId);
        return R.ok().setData(coupons);
    }

    @PostMapping("/save/couponHistory")
    public R saveCouponHistory(@RequestBody CouponAndMemberTo couponAndMemberTo) {
        try {
            MemberRespVo memberRespVo = couponAndMemberTo.getMemberRespVo();
            CouponTo couponTo = couponAndMemberTo.getCouponTo();
            couponHistoryService.saveOrUpdateCouponHistory(memberRespVo, couponTo);
        } catch (Exception e) {
            return R.error().setData(e.getMessage());
        }
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = couponHistoryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        CouponHistoryEntity couponHistory = couponHistoryService.getById(id);

        return R.ok().put("couponHistory", couponHistory);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CouponHistoryEntity couponHistory) {
        couponHistoryService.save(couponHistory);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CouponHistoryEntity couponHistory) {
        couponHistoryService.updateById(couponHistory);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        couponHistoryService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
